package com.gxa.modules.sys.listener;

import com.gxa.modules.sys.service.CodeService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@RabbitListener(queues = "queue-delay-half")
public class MessageListener01 {

    @Autowired
    private CodeService codeService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RabbitHandler
    public  void receive01(Integer delay, Message message, Channel channel){

        MessagePostProcessor messagePostProcessor = new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setDelay(delay);
                return message;
            }
        };
        String msg = "在来一半";

        this.rabbitTemplate.convertAndSend("exchange-delay","delay-tt","msg",messagePostProcessor);


        try {
            //消息确认
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            String msg1 = null;
        }catch (Exception e){
            e.printStackTrace();
        }

        log.info("MessageListener02 消费到了延迟消息---msg-->{}",delay);
    }

}
